package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityLoginPasswordBinding
import com.haoshuang.zhouzhoubang.ui.vm.LoginPasswordViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  登录密码界面
 */
class LoginPasswordActivity :
    BaseVMActivity<LoginPasswordViewModel, ActivityLoginPasswordBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.clickWithTrigger { finish() }
        binding.appBar.tvTitle.text = "登录密码"
    }

    override fun initData() {
        binding.tvCommit.clickWithTrigger {
            var password = binding.tvPassword.text.toString()
            var newPwd = binding.newPwd.text.toString()
            if (password.isEmpty()) {
                toast("请输入新密码")
                return@clickWithTrigger
            }
            if (newPwd != password) {
                toast("两次密码不一致, 请重新输入")
                return@clickWithTrigger
            }
            vm.getData("new_pwd" to newPwd)
        }
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                finish()
            }
        }
    }

}